package mylist;

import java.util.Arrays;

public class Dome implements Ilist{
    public int[] elem;
    public int userSize;

    public static final int  DEFAULT_SIZE=10;
    public  Dome(){
        this.elem=new int[DEFAULT_SIZE];
    }
    public  Dome(int capacity){
        this.elem=new int[capacity];

    }
    @Override
    public void add(int data) {
        checkElem();

        this.elem[userSize] = data;
        this.userSize++;
    }



    @Override
    public void add(int pos, int data) {
        try{
            checkPosillegal(pos);
        }catch(PosIllegality e){
            e.printStackTrace();
            return;
        }
        checkElem();
        for(int i = userSize-1;i>=pos;i--){
            elem[i+1]=elem[i];
        }
        elem[pos] = data;
        userSize++;
    }
    private void checkPosillegal(int pos){
        if(pos<0||pos>this.userSize) {
            System.out.println("不合法");
            throw new PosIllegality("插入元素下标异常"+pos);
        }
    }
    private void checkElem(){
        if(isFull()){
            elem = Arrays.copyOf(elem,elem.length*2);
        }
    }
    @Override
    public boolean contains(int toFind) {
        if(isEmpty()){
            return false;
        }
        for(int i = 0;i<userSize;i++){
            if(elem[i]==toFind){
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean isEmpty() {
        if(userSize==0){
            return true;
        }
        return false;
    }
    @Override
    public int indexOf(int toFind) {
       if(isEmpty()){
           return -1;
       }
        for(int i = 0;i<userSize;i++){
            if(toFind==elem[i]){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) throws MyArrayListEmpty{
        checkPosillegal(pos);
        if(isEmpty()){
            throw new MyArrayListEmpty("获取下标元素为空"+pos);
        }

        return elem[pos];
    }

    @Override
    public void set(int pos, int value) {

    }

    @Override
    public void remove(int toRemove) {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        for(int i = 0;i<userSize;i++){
            System.out.print(elem[i]+" ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return  userSize==elem.length;
    }


}
